﻿#include <iostream>
using namespace std;


//实现一个只能在堆上创建的类
class HeapOnly
{
public:
	static HeapOnly* CreateObject()
	{
		return new HeapOnly;
	}
private:
	HeapOnly() {}

	// C++98
	// 1.只声明,不实现。因为实现可能会很麻烦，而你本身不需要
	// 2.声明成私有
	HeapOnly(const HeapOnly&);

		// or

	// C++11    
	HeapOnly(const HeapOnly&) = delete;
};

int main()
{
	HeapOnly* hp = HeapOnly::CreateObject();
	//HeapOnly hp1(hp);

	return 0;
}


class StackOnly
{
public:
	static StackOnly CreateObj()
	{
		return StackOnly();
	}

	// 禁掉operator new可以把下面用new 调用拷贝构造申请对象给禁掉
	// StackOnly obj = StackOnly::CreateObj();
	// StackOnly* ptr3 = new StackOnly(obj);
	void* operator new(size_t size) = delete;
	void operator delete(void* p) = delete;
private:
	StackOnly()
		:_a(0)
	{}
private:
	int _a;
};


int main()
{
	StackOnly st = StackOnly::CreateObj();
	StackOnly* ptr3 = &st;

	return 0;
}